/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cloudminga.gui;

import java.io.File;
import java.text.DecimalFormat;
import javax.swing.JCheckBox;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Lakhan
 */
/*class for geeting the BackupsetData*/
public class BackupSetView {

    long totalFilesCount = 0;
    int totalFile = 0;
    long filesize = 0;
    long updateTotalFile = 0;
    long foldersize = 0;
    long usedSize = 0;
    double quota;
    double quotaPercent = 0.0;
    DefaultTableModel model;
    JCheckBox checkbox;
    private static BackupSetView backupSetViewInstance = null;
    private Object[][] filesDetails;
    public static BackupSetView getInstance() {
        if (backupSetViewInstance == null) {
            backupSetViewInstance = new BackupSetView();
        }
        return backupSetViewInstance;
    }

   private Object[][] getBackupSetFileDetails() {
        String source = System.getProperty("user.home");
        File[] Files = new File(source).listFiles();
        Object[][] obj = new Object[Files.length][4];
        for (int i = 0; i < Files.length; i++) {
            String size = null;
            filesize = 0;
            totalFile = 0;
            filesize = getFileSize(Files[i]);
            size = getSizeFormat(filesize);
            obj[i][0] = (Object)false;
            obj[i][1] = Files[i].getName();
            obj[i][2] = totalFile;
            obj[i][3] = size;

        }
        return obj;
    }
   public void printBackSet()
   {
       System.out.println("printing the backup sets");
       DefaultTableModel model = (DefaultTableModel) MainForm.backupsetLeftTable.getModel();
       int rows= model.getRowCount();
       for(int i=rows-1;i>=0;i--)
           model.removeRow(i);
       rows=filesDetails.length;
       for(int i=0;i<rows;i++)
       {
           model.addRow(new Object[]{Boolean.parseBoolean(filesDetails[i][0].toString()),filesDetails[i][1],filesDetails[i][2],filesDetails[i][3]});
       }

   }

    private BackupSetView() {
        filesDetails=getBackupSetFileDetails();
    }

    public long getFileSize(File folder) {
        foldersize = 0;
        updateTotalFile = 0;
        if (folder.isDirectory()) {

            File[] filelist = folder.listFiles();
            if (filelist != null) {
                for (int i = 0; i < filelist.length; i++) {

                    if (filelist[i].isDirectory()) {
                        foldersize += getFileSize(filelist[i]);
                    } else {
                        totalFile++;
                        updateTotalFile++;
                        foldersize += filelist[i].length();
                    }
                }
            }
        } else {
            totalFile++;
            updateTotalFile++;
            foldersize = foldersize + folder.length();
        }
        return foldersize;
    }

    public String getSizeFormat(long filesizes) {

        DecimalFormat dfs = new DecimalFormat("#.##");//To display values upto 2 decimal point
        String sizes = null;
        double kb = 0.0, gb = 0.0, mb = 0.0, tempSize = 0.0;
        if (filesizes < 1024) {
            sizes = String.valueOf(filesizes) + "" + " bytes";
        } else if (filesizes >= 1024 && filesizes < (1024 * 1024)) {
            tempSize = filesizes;
            kb = tempSize / 1024;
            sizes = String.valueOf(dfs.format(kb)) + " " + "KB";

        } else if (filesizes >= (1024 * 1024) && filesizes < (1024 * 1024 * 1024)) {
            tempSize = filesizes / 1024;
            mb = tempSize / (1024);
            //  mb = filesizes / (1024 * 1024);

            sizes = String.valueOf(dfs.format(mb)) + " " + "MB";

        } else if (filesizes >= (1024 * 1024 * 1024)) {

            tempSize = filesizes / (1024 * 1024);
            gb = tempSize / (1024);
            sizes = String.valueOf(dfs.format(gb)) + " " + "GB";
        }
        return sizes;
    }
}








